package com.tces.demo.controller;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.tces.demo.domain.UploadVideo;
import com.tces.demo.service.UploadVideoService;

@Controller
public class UploadController {
	
	@Autowired
	private UploadVideoService uploadVideoService;
	/**
	 * 上传页面
	 * @return 视图
	 */
	@RequestMapping("/upload")
	public ModelAndView upload(){
		ModelAndView mv=new ModelAndView("upload");
		return mv;
	}
	
	/**
	 * 上传视频到文件服务器
	 * @param upload 视频文件
	 * @param seconds 总秒数
	 * @param model 
	 * @return 视图
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@RequestMapping("/upload_submit")
	public ModelAndView upload_submit(@RequestParam("upload")MultipartFile upload,Integer seconds,Model model) throws IllegalStateException, IOException{
		//设置上传服务器的路径 要保证有loads这个目录
        String path = "http://106.54.235.164:8080/uploads/";
 
        //获取上传文件的名称
        String originalName = upload.getOriginalFilename();
        //把文件名设置成一个唯一值
        String uid = UUID.randomUUID().toString().replace("-","");
 
        String filename = uid + "_" + originalName;
        //创建客户端对象
        Client client = Client.create();
        //与服务器进行连接(解决中文乱码，无法上传问题)
        WebResource webResource = client.resource(path + URLEncoder.encode(filename,"utf-8"));
        //进行文件上传，并将文件转换成字节流
        webResource.put(upload.getBytes());
        
        //保存到数据库
        UploadVideo video=new UploadVideo();
        video.setVideoName(originalName);
        video.setUrl(filename);
        video.setTotalSeconds(seconds);
        uploadVideoService.add(video);
        return new ModelAndView("redirect:/list");
	}
	
	/**
	 * 视频列表页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView success(Model model){
		List<UploadVideo> videos=uploadVideoService.getList();
		model.addAttribute("videos", videos);
		ModelAndView mv=new ModelAndView("list");
		return mv;
	}
	
	/**
	 * 视频播放
	 * @param id
	 * @return
	 */
	@RequestMapping("/videoPlay")
	public ModelAndView videoPlay(int id){
		ModelAndView mv=new ModelAndView("videoPlay");
		UploadVideo video=uploadVideoService.getOneById(id);
		mv.addObject("src", "http://106.54.235.164:8080/uploads/"+video.getUrl());
		mv.addObject("video", video);
		return mv;
	}
	
	/**
	 * 记录视频播放进度
	 * @param id
	 * @param curTime
	 * @param model
	 * @return
	 */
	@RequestMapping("/record")
	@ResponseBody
	public String record(int id,int curTime,Model model){
		uploadVideoService.updateCurrentTimeById(curTime, id);
		return "记录成功";
	}
}
